//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    latitude: 40.84212,
    longitude: 111.748823,
    icons:[]
  },

  onLoad(){
    wx.showLoading({
      title: '加载中...',
    })
  },

  onShow() {
    this.loadBanner()
    this.loadNews()
    this.loadStation()
    this.loadIcons()
  },

  // 轮播图
  loadBanner() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/bannerlist',
      data: {
        access_token: access_token,
      },
      success: (res) => {
        console.log(res)
        if(res.data.code >= 500){
          app.getLogin().then(function(){
            console.log('二次登录。。。')
          })
        } else if (res.data.code == 1) {
          this.setData({
            bannerList: res.data.list
          })
        }
      }
    })
  },

  // 新闻列表
  loadNews() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/newslist',
      data: {
        access_token: access_token,
        news_num: 3
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            newsList: res.data.list
          })
        }
      }
    })
  },

  // 地图
  loadStation() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/stationlist',
      data: {
        access_token: access_token,
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          // this.setData({
          //   locationArr: res.data.list
          // })
          let markers = res.data.list.map(item => {
            return {
              id: item.id,
              longitude: item.x_coordinate,
              latitude: item.y_coordinate,
              iconPath: "../../static/icons/location.png",
              name:item.department_name
            }
          })
          console.log(markers)
          for (let i in markers) {
            markers[i].label = {
              content: markers[i].name,
              color: '#a50a1d',  
              borderRadius: 3,  
              borderWidth: 1,  
              borderColor: '#a50a1d',  
              bgColor: '#ffffff',  
              padding: 5, 
              textAlign: 'center'
            }
          }
          this.setData({
            markers:markers
          })
        }
      },
      complete:(res)=>{
        wx.hideLoading()
      }
    })
  },
  
  // 图标
  loadIcons(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://retire.wshoto.com/wx/buttonlist',
      data:{
        access_token:access_token
      },
      success:(res)=>{
        console.log(res)
        if(res.data.code == 1){
          this.setData({
            icons:res.data.list
          })
        }
      }
    })
  },

  toNewsDetail(e) {
    let news_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../news-details/news-details?news_id=' + news_id,
    })
  },

  toModuleDetail(e){
    let {id,name} = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/details/details?name=' + name + '&type_id=' + id,
    })
  },

  newsMore() {
    wx.navigateTo({
      url: '../more-news/more-news',
    })
  },

  onReady: function (e) {
    this.mapCtx = wx.createMapContext('map')
  },

  tap(e) {
    console.log(e)
    let markers = this.data.markers
    let jingdu = ''
    let weidu = ''
    let name = ''
    let markerId = e.detail.markerId
    for (let item of markers) {
      if (item.id == markerId) {
        jingdu = item.longitude
        weidu = item.latitude
        name = item.label.content
        wx.getLocation({
          type: 'gcj02',
          success: (res) => {
            wx.openLocation({
              latitude: Number(weidu)  ,
              longitude: Number(jingdu)  ,
              name: name,
              success: (res) => {  
                console.log(jingdu)
                console.log(weidu)
              }
            })
          }
        })
        break
      }
    }
  }
})
